﻿using System.IO.Ports;
using System.Text;
using Oxage.CNC;
using Oxage.Windows.Controls;
using Arguments = Oxage.Windows.Controls.Arguments;

namespace Oxage.Commands
{
	[CommandLineHandler("g", "Sends a line of G code")]
	public class GCodeLineCommandLineHander : ICommandLineHandler
	{
		#region Private members
		private static bool wrap = false;
		#endregion

		#region ICommandLineHandler Members
		public void Run(IConsole console, string line, string command, Arguments args)
		{
			if (args.Count == 0 || args.Help)
			{
				console.WriteLine("Sends a line of G code");
				console.WriteLine();
				console.WriteLine("Usage:");
				console.WriteLine("  g [line]");
				console.WriteLine();
				console.WriteLine("Examples:");
				console.WriteLine("  g G91");
				console.WriteLine("  g G01 X10 Y20 Z30");
				console.WriteLine("  g G91\\nG01 X10 Y20 Z30");
				return;
			}

			//TODO: Wrap line of G code to JSON/BSON object
			//TODO: Select device: serial port or simulator

			if (args.Count > 0)
			{
				string gcode = line.Substring(command.Length + 1).Replace("\\n", "\n");
				App.ViewModel.MachineController.Write(Encoding.ASCII.GetBytes(gcode));
				console.WriteLine(gcode);
			}
		}
		#endregion
	}
}
